using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using Sustainalytics.ESGRatings.Computing.Service.Controllers;
using Sustainalytics.ESGRatings.BusinessLogic.Computing;
using Sustainalytics.ESGRatings.Computing.Service.Infrastructure;
using Sustainalytics.ESGRatings.DataLayer;
using System.Configuration;
using Sustainalytics.ESGRatings.Computing.Service.Helpers;
using Sustainalytics.MongoDB.Extensions;
using Serilog;

namespace Sustainalytics.ESGRatings.Computing.Service
{

    public class CompositionRoot : IHttpControllerActivator
    {
        private readonly HashSet<Type> _registeredApiControllers = new HashSet<Type>
                                                                        {
                                                                            typeof(ESGComputeController),
                                                                            typeof(GovernanceComputeController),
                                                                            typeof(GovernanceDataContainerController),
                                                                            typeof(EsgDataContainerController),
                                                                            typeof(EsgIndicatorTemplatesController),
                                                                            typeof(EsgRawScoresController),
                                                                            typeof(EsgReferenceUniverseController),
                                                                            typeof(EsgReferenceUniverseController),
                                                                            typeof(EsgWeightsController),
                                                                            typeof(EsgComputationHealthController)
                                                                        };

        public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
        {
            if (this._registeredApiControllers.Contains(controllerType))
            {
                if (controllerType == typeof(EsgDataContainerController))
                {
                    var config = ConfigurationManager.GetSection("ServiceAutoProvisioningGroup/ServiceAutoProvisioning");
                    var configForSelfCare = config as ServiceAutoProvisioningSection;

                    var dataLoader = new ESGSetLoader(
                            MongoFactory.GetCollectionFromDatabase<HistoricalDataSet>(configForSelfCare.ConnectionString),
                            MongoFactory.GetCollectionFromDatabase<DSCorporateData>(configForSelfCare.ConnectionString),
                            MongoFactory.GetCollectionFromDatabase<DSCompanyRawScores>(configForSelfCare.ConnectionString),
                            MongoFactory.GetCollectionFromDatabase<DSIndicatorTemplate>(configForSelfCare.ConnectionString),
                            MongoFactory.GetCollectionFromDatabase<DSWeightMatrix>(configForSelfCare.ConnectionString),
                            MongoFactory.GetCollectionFromDatabase<DSReferenceUniverse>(configForSelfCare.ConnectionString));

                    return (IHttpController)Activator.CreateInstance(controllerType, ComputationEngine.Instance((string template, Exception ex) => { Log.Error(ex, template); }), dataLoader);
                }

                return (IHttpController)Activator.CreateInstance(controllerType, ComputationEngine.Instance((string template, Exception ex) => { Log.Error(ex, template); }));

            }

            throw new ArgumentException(string.Format("Unable to locate controller of type {0}", controllerType));
        }
    }
}
